package com.yamaha.bikemarket;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.yamaha.bikemarket.util.LogUtil;
import com.yamaha.bikemarket.util.SCStaticFunc;

public class ShowByFragment extends AMyActivity {
	ViewGroup container;

	public ShowByFragment() {
		super(MenuType.SHOW_ALL_BIKE);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showby_layout);

		final TextView brandModelText = (TextView) findViewById(R.id.bandmodel_text);
		final TextView showAllText = (TextView) findViewById(R.id.showall_text);
		final TextView likeText = (TextView) findViewById(R.id.like_text);
		final TextView regYear = (TextView) findViewById(R.id.regyear_text);
		final TextView eventText = (TextView) findViewById(R.id.event_text);
		final TextView friendText = (TextView) findViewById(R.id.friend_text);
		final TextView userOnly = (TextView) findViewById(R.id.user_only);
		final TextView dealerOnly = (TextView) findViewById(R.id.dealer_only);
		final TextView province = (TextView) findViewById(R.id.province);

		brandModelText.setOnClickListener(this);
		showAllText.setOnClickListener(this);
		likeText.setOnClickListener(this);
		regYear.setOnClickListener(this);
		eventText.setOnClickListener(this);
		friendText.setOnClickListener(this);
		userOnly.setOnClickListener(this);
		dealerOnly.setOnClickListener(this);
		province.setOnClickListener(this);

		setListenerForMenu();

	}

	@Override
	protected List<EditText> getEditTexts() {
		return null;
	}

	@Override
	public void onClick(View v) {

		if (onMenuClick(v)) {
			return;
		}

		switch (v.getId()) {
		case R.id.bandmodel_text: {
			LogUtil.i("ShowByFragment", "User selected Brand-Model Menu.");

			final Intent intent = new Intent(ShowByFragment.this,
					BrandModelFragment.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(intent);

		}
			break;
		case R.id.showall_text: {
			LogUtil.i("ShowByFragment", "User selected Show All Menu.");
			final Intent intent = new Intent(ShowByFragment.this,
					MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(intent);
		}
			break;
		case R.id.regyear_text: {
			LogUtil.i("ShowByFragment", "User selected Reg Year Menu.");
			final AlertDialog.Builder builder = new AlertDialog.Builder(this);

			final List<String> year = new ArrayList<String>();
			for (int i = 2000; i < 2050; i++) {
				year.add("" + i);
			}

			builder.setItems(year.toArray(new String[year.size()]),
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

							final int selectedYear = which + 2000;

							final Set<Integer> regYear = new HashSet<Integer>();
							regYear.add(selectedYear);

							final Intent intent = new Intent(
									ShowByFragment.this, MainActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
							intent.putExtra("filterType", "REG_YEAR");
							final JSONArray array = new JSONArray(regYear);
							intent.putExtra("filterParams", array.toString());
							startActivity(intent);
						}
					});
			builder.create().show();

		}
			break;
		case R.id.province: {
			LogUtil.i("ShowByFragment", "User selected Province Menu.");
			try {
				final AlertDialog.Builder builder = new AlertDialog.Builder(
						this);

				final JSONArray province = (JSONArray) SCStaticFunc.getData(
						this, SCStaticFunc.PROVINCE);
				final List<String> p = new ArrayList<String>();
				for (int i = 0; i < province.length(); i++) {
					final JSONObject obj = province.getJSONObject(i);
					p.add(obj.getString("PROVINCE_NAME"));
				}

				builder.setItems(p.toArray(new String[p.size()]),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								try {
									final JSONObject obj = province
											.getJSONObject(which);

									final int pId = obj.getInt("PROVINCE_ID");

									final Set<Integer> province = new HashSet<Integer>();
									province.add(pId);

									final Intent intent = new Intent(
											ShowByFragment.this,
											MainActivity.class);
									intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
									intent.putExtra("filterType", "PROVINCE");
									final JSONArray array = new JSONArray(
											province);
									intent.putExtra("filterParams",
											array.toString());
									startActivity(intent);
								} catch (JSONException e) {
									LogUtil.e("ShowByFragment", e.getMessage(),
											e);
								}

							}
						});
				builder.create().show();
			} catch (JSONException e) {
				LogUtil.e("ShowByFragment", e.getMessage(), e);
			}

		}
			break;
		case R.id.event_text: {
			LogUtil.i("ShowByFragment", "User selected Event Menu.");
			try {
				final JSONObject object = new JSONObject();
				object.put("param", "EVENT");

				SCStaticFunc.setData(ShowByFragment.this, "param", object);

				final Intent intent = new Intent(ShowByFragment.this,
						MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(intent);

			} catch (JSONException e) {
				LogUtil.e("ShowByFragment", e.getMessage(), e);
			}
		}
			break;
		case R.id.friend_text: {
			LogUtil.i("ShowByFragment", "User selected Friend Menu.");
			try {
				final JSONObject object = new JSONObject();
				object.put("param", "FRIEND");

				SCStaticFunc.setData(ShowByFragment.this, "param", object);

				final Intent intent = new Intent(ShowByFragment.this,
						MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(intent);

			} catch (JSONException e) {
				LogUtil.e("ShowByFragment", e.getMessage(), e);
			}
		}
			break;
		case R.id.user_only: {
			LogUtil.i("ShowByFragment", "User selected User-Only Menu.");
			try {
				final JSONObject object = new JSONObject();
				object.put("param", "USER_ONLY");

				SCStaticFunc.setData(ShowByFragment.this, "param", object);

				final Intent intent = new Intent(ShowByFragment.this,
						MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				intent.putExtra("filterType", "DEALER_ONLY");
				startActivity(intent);

			} catch (JSONException e) {
				LogUtil.e("ShowByFragment", e.getMessage(), e);
			}
		}
			break;
		case R.id.dealer_only: {
			LogUtil.i("ShowByFragment", "User selected Dealer-Only Menu.");
			try {
				final JSONObject object = new JSONObject();
				object.put("param", "DEALER_ONLY");

				SCStaticFunc.setData(ShowByFragment.this, "param", object);

				final Intent intent = new Intent(ShowByFragment.this,
						MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				intent.putExtra("filterType", "DEALER_ONLY");
				startActivity(intent);

			} catch (JSONException e) {
				LogUtil.e("ShowByFragment", e.getMessage(), e);
			}
		}
			break;
		}
	}
}
